package dimension.starry.loot;

import dimension.starry.utils.RandomUtil;
import net.minecraft.item.Item;
import net.minecraft.item.Items;

import java.util.HashMap;
import java.util.Map;

public class FoodLoots {

    private static final Map<Item, Integer> LOOT_WEIGHT_MAP = new HashMap<>() {{
        put(Items.TROPICAL_FISH, 100);
        put(Items.AIR, 5000);
        put(Items.APPLE, 100);
        put(Items.BAKED_POTATO, 100);
        put(Items.BEEF, 100);
        put(Items.BOWL, 200);
        put(Items.BREAD, 100);
        put(Items.CAKE, 100);
        put(Items.CHICKEN, 100);
        put(Items.COOKED_BEEF, 100);
        put(Items.COOKIE, 100);
        put(Items.COOKED_CHICKEN, 100);
        put(Items.COOKED_COD, 100);
        put(Items.COOKED_MUTTON, 100);
        put(Items.COOKED_PORKCHOP, 100);
        put(Items.COOKED_RABBIT, 100);
        put(Items.COOKED_SALMON, 100);
        put(Items.EGG, 100);
        put(Items.ENCHANTED_GOLDEN_APPLE, 1);
        put(Items.GLASS_BOTTLE, 100);
        put(Items.HONEY_BOTTLE, 100);
        put(Items.MELON, 100);
        put(Items.PORKCHOP, 100);
        put(Items.RABBIT, 100);
        put(Items.WHEAT, 100);
        put(Items.BEETROOT, 100);
        put(Items.PUMPKIN, 100);
        put(Items.PUMPKIN_PIE, 100);
        put(Items.SWEET_BERRIES, 100);
        put(Items.GLOW_BERRIES, 10);
        put(Items.TORCHFLOWER_SEEDS, 10);
        put(Items.POISONOUS_POTATO, 200);
        put(Items.CARROT, 100);
        put(Items.GOLDEN_CARROT, 10);
    }};

    public static Item getRandomItem() {
        int totalWeight = LOOT_WEIGHT_MAP.values().stream().mapToInt(i -> i).sum();
        int random = RandomUtil.randomInt(1, totalWeight + 1), current = 0;
        for (Map.Entry<Item, Integer> entry : LOOT_WEIGHT_MAP.entrySet()) {
            if (random > current && random <= current + entry.getValue()) {
                return entry.getKey();
            }
            current += entry.getValue();
        }
        throw new RuntimeException(String.format("calculate error, weight not in range: total=%d random=%d current=%d",
            totalWeight,
            random,
            current
        ));
    }

}
